<?php ob_start();?>
<?php 
$root = $_SERVER['DOCUMENT_ROOT'];
include($root.'/standart/head.php');
include($root.'/standart/dateHeader.php');
include($root.'/standart/dbconnect.php');
session_set_cookie_params(2*7*24*60*60);
// Making the cookie live for 2 weeks

session_start();
$db = connect();

$err = array();
// Will hold our errors


if(!$_POST['username'] || !$_POST['password'])
	$err[] = 'Lütfen tüm alanları eksiksiz doldurunuz!';
if(!count($err))
{
// 	$_POST['username'] = mysql_real_escape_string($_POST['username']);
// 	$_POST['password'] = mysql_real_escape_string($_POST['password']);
// 	$_POST['rememberMe'] = (int)$_POST['rememberMe'];

	// Escaping all input data
	$userCheckStmt = "SELECT USER_ID,CONCAT(NAME,SURNAME) AS MAIL FROM GEVEZE_USER, PASS_USER WHERE ISACTIVE = 'Y' AND GEVEZE_USER.ID = USER_ID AND EMAIL='{$_POST['username']}' AND PASSWORD='".md5($_POST['password'])."'";
	$row = $db->query($userCheckStmt)->fetch_assoc();
	if($row['MAIL']){
		// If everything is OK login
			
		$_SESSION['usr']=$row['MAIL'];
		$_SESSION['id'] = $row['USER_ID'];
		$_SESSION['rememberMe'] = $_POST['rememberMe'];
			
		// Store some data in the session
		if(isset($_POST['rememberMe'])){
			setcookie('tzRemember',$_POST['rememberMe']);
			setcookie('gevezeMail',$row['MAIL']);
			setcookie('gevezeUser',$row['USER_ID']);
		}else{
			setcookie("tzRemember","", time() -3600);
			setcookie("gevezeMail","", time() -3600);
			setcookie("gevezeUser","", time() -3600);
		}
	}
	else $err[]='Hatalı email adresi ve/veya şifre!';
}

if($err){
	$error = implode('<br/>',$err);
	?>
	<div id="loginErrorOccured">
		<?php echo $error ?>
	</div>
	<?php 
// Save the error messages in the session
}else{
?>
<div id="panel">
	<div class="content clearfix">
		<div class="right">
		<input class="btn btn-danger btn-xl" type="button" style="cursor: pointer; right: 0px; float: right; line-height: 12px; padding-right:30px; padding-left:30px;" onclick="window.location.replace('<?php echo $_POST['pageName']; ?>?logoff')" value="Çıkış"/>
            <div class="loginTop"><?php echo $_SESSION['usr'] ? 'Merhaba '.$_SESSION['usr'] : '';?> <br/></div>
            <div class="loginMenu">
            	<div class="loginMenuLeft" onclick="window.location.replace('order-list.php')"> Siparişlerim </div>
            	<div class="loginMenuMid" onclick="window.location.replace('user-info-change.php')"> Kişisel Bilgilerim </div>
            	<div class="loginMenuMid" onclick="window.location.replace('add-address.php')"> Adres Ekle</div>
            	<div class="loginMenuRight" onclick="window.location.replace('change-password.php')"> Şifre Değiştir </div>
            </div>
		</div>
	</div>
</div> <!-- /login -->	

    <!-- The tab on top -->	
<div class="tab">
	<ul class="login">
		<li class="left">&nbsp;</li>
	    <li id="toggle">
			<a id="open" class="open" href="#"><?php echo $_SESSION['id']?'Kullanıcı Bilgileri':'Üye Girişi | Yeni Üye';?></a>
			<a id="close" style="display: none;" class="closex" href="#">Bilgi Alanını Kapat</a>			
		</li>
	    <li class="right">&nbsp;</li>
	</ul> 
</div>
<?php }?>
<?php ob_end_flush(); ?>